package com.kaiyidiannao.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.SectionIndexer;
import android.widget.TextView;
import com.kaiyidiannao.common.util.SortModel;
import com.kaiyidiannao.data.DataCenter;
import com.kaiyidiannao.main.R;

import java.util.List;

public class SortAdapter extends BaseAdapter implements SectionIndexer{
    private List<SortModel> list = null;
    private Context mContext;
    private LayoutInflater layoutInflater;

    public SortAdapter(Context mContext, List<SortModel> list) {
        this.mContext = mContext;
        this.list = list;
        this.layoutInflater = LayoutInflater.from(mContext);
    }

    /**
     * 当ListView数据发生变化时,调用此方法来更新ListView
     * @param list
     */
    public void updateListView(List<SortModel> list){
        this.list = list;
        notifyDataSetChanged();
    }

    public int getCount() {
        return list == null ? 0 : list.size();
    }

    public Object getItem(int position) {
        return list.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View view, ViewGroup arg2) {
//        Log.d("postion--",position+"-=-=-=-=-=-=-=-");
//        Log.d("postion--",10%11+"=-=-=-=-=-=-=-=-=");
        ViewHolder viewHolder = null;
        ImageView itemIv = null;
        TextView title = null;
        final SortModel mContent = list.get(position);
        if (DataCenter.lmap.get(position) == null) {
            viewHolder = new ViewHolder();
            view = layoutInflater.inflate(R.layout.name_item, null);
            viewHolder.tvTitle = (TextView) view.findViewById(R.id.title);
            viewHolder.tvLetter = (TextView) view.findViewById(R.id.catalog);
            DataCenter.lmap.put(position,view);
            view.setTag(viewHolder);
        } else {
            view = DataCenter.lmap.get(position);
            viewHolder = (ViewHolder) view.getTag();
        }
        itemIv = (ImageView)view.findViewById(R.id.itemIv);
        title = (TextView)view.findViewById(R.id.title);
        for(int i = 0; i<=position;i++){
            if(i==DataCenter.index) {
                title.setTextColor(view.getResources().getColor(R.color.title_detail_color));
                itemIv.setVisibility(View.VISIBLE);
            }else{
                itemIv.setVisibility(View.GONE);
                title.setTextColor(Color.BLACK);
            }
        }
        //根据position获取分类的首字母的Char ascii值
        int section = getSectionForPosition(position);

        //如果当前位置等于该分类首字母的Char的位置 ，则认为是第一次出现
        if(position == getPositionForSection(section)){
            viewHolder.tvLetter.setVisibility(View.VISIBLE);
            viewHolder.tvLetter.setText(mContent.getSortLetters());
        }else{
            viewHolder.tvLetter.setVisibility(View.GONE);
        }

        viewHolder.tvTitle.setText(this.list.get(position).getName());

        return view;

    }



    final static class ViewHolder {
        TextView tvLetter;
        TextView tvTitle;
    }

    /**
     * 根据ListView的当前位置获取分类的首字母的Char ascii值
     */
    public int getSectionForPosition(int position) {
        return list.get(position).getSortLetters().charAt(0);
    }

    /**
     * 根据分类的首字母的Char ascii值获取其第一次出现该首字母的位置
     */
    public int getPositionForSection(int section) {
        for (int i = 0; i < getCount(); i++) {
            String sortStr = list.get(i).getSortLetters();
            char firstChar = sortStr.toUpperCase().charAt(0);
            if (firstChar == section) {
                return i;
            }
        }

        return -1;
    }

    /**
     * 提取英文的首字母，非英文字母用#代替。
     *
     * @param str
     * @return
     */
    private String getAlpha(String str) {
        String  sortStr = str.trim().substring(0, 1).toUpperCase();
        // 正则表达式，判断首字母是否是英文字母
        if (sortStr.matches("[A-Z]")) {
            return sortStr;
        } else {
            return "#";
        }
    }

    @Override
    public Object[] getSections() {
        return null;
    }
}